/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sysschool.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.sysschool.entity.SysSchool;
import com.thinkgem.jeesite.modules.sysschool.service.SysSchoolService;

/**
 * 学校管理Controller
 * @author lihuan
 * @version 2018-11-28
 */
@Controller
@RequestMapping(value = "${adminPath}/sysschool/sysSchool")
public class SysSchoolController extends BaseController {

	@Autowired
	private SysSchoolService sysSchoolService;
	
	@Autowired
	private OfficeService  officeService;
	
	@ModelAttribute
	public SysSchool get(@RequestParam(required=false) String id) {
		SysSchool entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysSchoolService.get(id);
		}
		if (entity == null){
			entity = new SysSchool();
		}
		return entity;
	}
	
	@RequiresPermissions("sysschool:sysSchool:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysSchool sysSchool, HttpServletRequest request, HttpServletResponse response, Model model) {
		Office office  = officeService.get(UserUtils.getUser().getOffice());
		if(StringUtils.isBlank(sysSchool.getParentCode())){
			sysSchool.setParentCode(office.getCode());
			sysSchool.setParentName(office.getName());
		}
		Page<SysSchool> page = sysSchoolService.findPage(new Page<SysSchool>(request, response), sysSchool); 
		model.addAttribute("sysSchool", sysSchool);
		model.addAttribute("page", page);
		model.addAttribute("office", office);
		return "modules/sysschool/sysSchoolList";
	}

	@RequiresPermissions("sysschool:sysSchool:view")
	@RequestMapping(value = "form")
	public String form(SysSchool sysSchool, Model model) {
		Office office = UserUtils.getUser().getOffice();
		if(StringUtils.isBlank(sysSchool.getId())){
			sysSchool.setParentCode(office.getCode());
			sysSchool.setParentName(office.getName());
		}
		sysSchool.setSchCode(sysSchoolService.getSchCode(sysSchool));
		model.addAttribute("sysSchool", sysSchool);
		return "modules/sysschool/sysSchoolForm";
	}
	
	@RequiresPermissions("sysschool:sysSchool:view")
	@RequestMapping(value = "addForm")
	public String addForm(SysSchool sysSchool, Model model) {
		sysSchool.setParentCode(sysSchool.getSchCode());
		sysSchool.setParentName(sysSchool.getSchName());
		sysSchool.setSchCode(sysSchoolService.getSchCode(sysSchool));
		model.addAttribute("sysSchool", sysSchool);
		return "modules/sysschool/sysSchoolForm1";
	}

	@RequiresPermissions("sysschool:sysSchool:edit")
	@RequestMapping(value = "save")
	public String save(SysSchool sysSchool, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sysSchool)){
			return form(sysSchool, model);
		}
		Office office = officeService.get(UserUtils.getUser().getOffice());
		SysSchool school = sysSchoolService.findByCode(sysSchool.getParentCode());
		if(school == null){
			sysSchool.setParentId(office.getId());
		}else{
			sysSchool.setParentId(school.getId());
		}
		sysSchoolService.save(sysSchool);
		addMessage(redirectAttributes, "保存学校管理成功");
		return "redirect:"+Global.getAdminPath()+"/sysschool/sysSchool/";
	}
	
	@RequiresPermissions("sysschool:sysSchool:edit")
	@RequestMapping(value = "delete")
	public String delete(SysSchool sysSchool, RedirectAttributes redirectAttributes) {
		sysSchoolService.delete(sysSchool);
		addMessage(redirectAttributes, "删除学校管理成功");
		return "redirect:"+Global.getAdminPath()+"/sysschool/sysSchool/?repage";
	}
	
	/**
	 * @author lihuan
	 * @date 2018年11月28日 下午3:44:39
	 * @description 查询学校树形列表
	 */
	 @RequestMapping(value = "schTreeData")
	 @ResponseBody
	 public List<Map<String, Object>> schTreeData(SysSchool sysSchool){
		 Office off = officeService.get(UserUtils.getUser().getOffice().getId());
		 sysSchool.setParentCode(off.getCode());
		 Office office = officeService.get(off.getId());
		 Map<String, Object> oMap = new HashMap<>();
		 oMap.put("id", office.getId());
		 oMap.put("pId", "0");
		 oMap.put("name", office.getName());
		 oMap.put("code", office.getCode());
		 List<Map<String, Object>> mapList = new ArrayList<>();
		 List<SysSchool> list = sysSchoolService.findList(sysSchool);
		 for(int i=0;i<list.size();i++){
			 SysSchool ss = list.get(i);
			 if(off.getCode().equals(ss.getSchCode())){
				 continue;
			 }
			 Map<String, Object> map = Maps.newHashMap();
			 map.put("id", ss.getId());
			 map.put("pId", ss.getParentId());
			 map.put("name", ss.getSchName());
			 map.put("code", ss.getSchCode());
			 mapList.add(map);
		 }
		 mapList.add(oMap);
		return mapList;
	 }
	 
	@RequestMapping(value = "getSchCode")
	@ResponseBody
	public SysSchool getSchCode(@RequestParam(value =  "code" , required = false, defaultValue = "")String code, 
			HttpServletRequest request, HttpServletResponse response) {
		SysSchool school = sysSchoolService.findByCode(code);
		return school;
	}
}